//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AnalyticEuropeanMargrabeEngine.h"
using namespace Cephei::QL::Experimental::Exoticoptions;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process2, Double correlation) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess1;
    CGeneralizedBlackScholesProcess^ _Cprocess2;
    try
    {
#ifdef HANDLE
        _phAnalyticEuropeanMargrabeEngine = NULL;
#endif
        _Cprocess1 = safe_cast<CGeneralizedBlackScholesProcess^> (process1);
        _Cprocess1->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process1 = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess1->GetShared ()); 
        _Cprocess2 = safe_cast<CGeneralizedBlackScholesProcess^> (process2);
        _Cprocess2->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process2 = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess2->GetShared ()); 
        QuantLib::Real _correlation = (QuantLib::Real)ValueHelper::Convert (correlation); //d
        _ppAnalyticEuropeanMargrabeEngine = new boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine> (new QuantLib::AnalyticEuropeanMargrabeEngine ( _process1,  _process2,  _correlation ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess1 != nullptr) _Cprocess1->Unlock();
        if (_Cprocess2 != nullptr) _Cprocess2->Unlock();
    }
}
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticEuropeanMargrabeEngine = NULL;
#endif
	_ppAnalyticEuropeanMargrabeEngine = &childNative;
    
}
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (QuantLib::AnalyticEuropeanMargrabeEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAnalyticEuropeanMargrabeEngine = NULL;
#endif
	_ppAnalyticEuropeanMargrabeEngine = new boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine> (&childNative);
    
    _AnalyticEuropeanMargrabeEngineOwner = owner;
    
}

Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (CAnalyticEuropeanMargrabeEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticEuropeanMargrabeEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticEuropeanMargrabeEngine = new boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticEuropeanMargrabeEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticEuropeanMargrabeEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (QuantLib::Handle<QuantLib::AnalyticEuropeanMargrabeEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAnalyticEuropeanMargrabeEngine = &childNative;
	_ppAnalyticEuropeanMargrabeEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine>>(childNative.currentLink());
    
    _AnalyticEuropeanMargrabeEngineOwner = owner;
}
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (QuantLib::Handle<QuantLib::AnalyticEuropeanMargrabeEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAnalyticEuropeanMargrabeEngine = &childNative;
	_ppAnalyticEuropeanMargrabeEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::CAnalyticEuropeanMargrabeEngine (QuantLib::AnalyticEuropeanMargrabeEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticEuropeanMargrabeEngine = NULL;
#endif
	_ppAnalyticEuropeanMargrabeEngine = new boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine> (new QuantLib::AnalyticEuropeanMargrabeEngine (childNative));
    
}
#endif

Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::~CAnalyticEuropeanMargrabeEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAnalyticEuropeanMargrabeEngine != NULL)
    {
	    delete _ppAnalyticEuropeanMargrabeEngine;
        _ppAnalyticEuropeanMargrabeEngine = NULL;
    }
}
Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::!CAnalyticEuropeanMargrabeEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAnalyticEuropeanMargrabeEngine != NULL)
    {
	    delete _ppAnalyticEuropeanMargrabeEngine;
    }
}
QuantLib::AnalyticEuropeanMargrabeEngine& Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::GetReference ()
{
    if (_ppAnalyticEuropeanMargrabeEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppAnalyticEuropeanMargrabeEngine;
}
boost::shared_ptr<QuantLib::AnalyticEuropeanMargrabeEngine>& Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::GetShared ()
{
    if (_ppAnalyticEuropeanMargrabeEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppAnalyticEuropeanMargrabeEngine;
}
QuantLib::AnalyticEuropeanMargrabeEngine* Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::GetPointer ()
{
    if (_ppAnalyticEuropeanMargrabeEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppAnalyticEuropeanMargrabeEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticEuropeanMargrabeEngine>& Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::GetHandle ()
{
	if (_phAnalyticEuropeanMargrabeEngine == NULL)
	{
		_phAnalyticEuropeanMargrabeEngine = new Handle<QuantLib::AnalyticEuropeanMargrabeEngine> (*_ppAnalyticEuropeanMargrabeEngine);
	}
	return *_phAnalyticEuropeanMargrabeEngine;
}
#endif
bool Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::HasNative () 
{
	return (_ppAnalyticEuropeanMargrabeEngine != NULL);
}

Cephei::QL::Experimental::Exoticoptions::IAnalyticEuropeanMargrabeEngine^ Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticEuropeanMargrabeEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Exoticoptions::IAnalyticEuropeanMargrabeEngine^ Cephei::QL::Experimental::Exoticoptions::CAnalyticEuropeanMargrabeEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process2, Double correlation)
{
    return REFNEW CAnalyticEuropeanMargrabeEngine ( process1,  process2,  correlation);
}
